package com.ylc.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylc.reggie.common.R;
import com.ylc.reggie.dto.DishDto;
import com.ylc.reggie.dto.SetmealDto;
import com.ylc.reggie.entity.*;
import com.ylc.reggie.service.CategoryService;
import com.ylc.reggie.service.DishService;
import com.ylc.reggie.service.SetmealDishService;
import com.ylc.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ylc
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 分页信息查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {}, pageSize = {}, name = {}", page, pageSize);

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();

        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //添加排序条件
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);

        //执行查询
        setmealService.page(pageInfo, queryWrapper);

        //对象拷贝,忽略records属性
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        //将获取的List<Dish>转为我们需要的List<DishDto>
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
           SetmealDto setmealDto = new SetmealDto();

           BeanUtils.copyProperties(item, setmealDto);
           //获取分类对象
            Category category = categoryService.getById(item.getCategoryId());

            if(category != null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }
    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }
    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        setmealService.removeWithDish(ids);

        return R.success("套餐数据删除成功");
    }

    /**
     * 根据id查询套餐信息和对应的菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.getByIdWithDish(id);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("修改套餐");

        setmealService.updateWithDish(setmealDto);

        return R.success("修改套餐信息成功");
    }
    /**
     * 套餐管理-起售停售
     * @param statusCode
     * @param ids
     * @return
     */
    @PostMapping("/status/{statusCode}")
    public R<String> updateStatus(@PathVariable Integer statusCode,
                                  @RequestParam List<Long> ids){
        log.info("改变套餐状态");

        List<Setmeal> setmeals = setmealService.listByIds(ids);
        setmeals.forEach(setmeal -> {
            setmeal.setStatus(statusCode);
        });
        //启售套餐的同时也起售套餐中的菜品
        if(statusCode == 1){
            //批量改变菜品状态
            List<SetmealDish> setmealDishes = setmealDishService.listByIds(ids);
            setmealDishes.forEach(setmealDish -> {
                Dish dish = dishService.getById(setmealDish.getDishId());
                dishService.updateById(dish);
            });
        }
        //启售套餐
        setmealService.updateBatchById(setmeals);
        return R.success("套餐状态修改成功");
    }
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        log.info("setmeal:{}", setmeal);
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(setmeal.getName()), Setmeal::getName, setmeal.getName());
        queryWrapper.eq(null != setmeal.getCategoryId(), Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(null != setmeal.getStatus(), Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        return R.success(setmealService.list(queryWrapper));
    }

}
